//
//  ShadowViewController.m
//  RACExample
//
//  Created by 山刘 on 2020/6/24.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "ShadowViewController.h"

@interface ShadowViewController ()
@property (weak, nonatomic) IBOutlet UISwitch *radiosSwitch;
@property (weak, nonatomic) IBOutlet UIView *greenView;
@property (weak, nonatomic) IBOutlet UISlider *offsetYSlider;
@property (weak, nonatomic) IBOutlet UISlider *offsetXSlider;
@property (weak, nonatomic) IBOutlet UISlider *shadowOpacitySlider;
@property (weak, nonatomic) IBOutlet UISlider *shadowRadiusSlider;
@property (weak, nonatomic) IBOutlet UITextView *showLabel;
@end

@implementation ShadowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self viewWillLayoutSubviews1];
    [self inserLayer];
}
- (IBAction)update:(id)sender {
    [self viewWillLayoutSubviews1];
}

-(void)viewWillLayoutSubviews1 {
    self.greenView.layer.cornerRadius = self.radiosSwitch.on ? 5 : 0;
//    self.greenView.layer.masksToBounds = self.radiosSwitch.on ? YES : NO;
    
    self.greenView.layer.shadowOffset = CGSizeMake(self.offsetXSlider.value, self.offsetYSlider.value);//    阴影偏移量
    self.greenView.layer.shadowOpacity = self.shadowOpacitySlider.value;//    阴影透明度
    self.greenView.layer.shadowRadius = self.shadowRadiusSlider.value;//    阴影半径
    self.greenView.layer.shadowColor = [UIColor blackColor].CGColor;
    
    self.showLabel.text = [NSString stringWithFormat:@"viewSize:%@\n shadowOffset:%@\n shadowOpacity:%f\n shadowRadius:%f\n",NSStringFromCGSize(self.greenView.bounds.size),NSStringFromCGSize(self.greenView.layer.shadowOffset),
    self.greenView.layer.shadowOpacity,
    self.greenView.layer.shadowRadius];
}

-(void)inserLayer {
    CALayer *shadowLayer0 = [[CALayer alloc] init];
    shadowLayer0.shadowColor = [UIColor blueColor].CGColor;
    shadowLayer0.shadowOpacity = self.shadowOpacitySlider.value;
    shadowLayer0.shadowOffset = CGSizeMake(self.offsetXSlider.value, self.offsetYSlider.value);
    shadowLayer0.shadowRadius = self.shadowRadiusSlider.value;
    shadowLayer0.frame = self.greenView.frame;
    [self.greenView.superview.layer insertSublayer:shadowLayer0 below:self.greenView.layer];
}
@end
